<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
        <el-input
          v-model="queryParams.unifiedSocialCreditCode"
          placeholder="请输入统一社会信用代码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择状态"
          clearable
          style="width: 240px"
        >
          <el-option :key="`1`" :label="`启用`" :value="1" />
          <el-option :key="`2`" :label="`停用`" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
          >新增</el-button
        >
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
         @change="operationFun"
          v-hasPermi="['system:role:remove']"
        >批量操作</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-select
          :disabled="multiple"
          v-model="operationValue"
          @change="operationFun"
          placeholder="批量操作"
        >
          <el-option
            v-for="item in stateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="supplierList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="number" width="120" />
      <el-table-column
        label="公司名称"
        prop="companyName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="统一社会信用代码"
        prop="unifiedSocialCreditCode"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="公司电话" prop="companyNumber" width="100" />
      <el-table-column label="法人姓名" prop="nameLegalPerson" width="100" />
      <el-table-column
        label="法人证件号"
        prop="legalPersonCertificateNo"
        width="100"
      />
      <el-table-column label="联系人姓名" prop="contactName" width="100" />
      <el-table-column
        label="联系人手机号码"
        prop="contactMobileNumber"
        width="100"
      />
      <el-table-column label="联系人邮箱" prop="contactEmail" width="100" />
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <span> {{ scope.row.state | dictFilter(statusList) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="founder" width="100" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['system:role:remove']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope)"
            v-hasPermi="['system:role:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- <add :info="info" :show="open" v-if="open" @close="closeFun"></add> -->
    <add
      :info="info"
      @sureData="sure"
      v-if="open"
      @close="closeFun"
      :detail="currentDetail"
    ></add>
  </div>
</template>

<script>
import {
  getRole,
  delRole,
  addRole,
  updateRole,
  changeRoleStatus,
} from "@/api/system/role";
import {
  treeselect as menuTreeselect,
  roleMenuTreeselect,
} from "@/api/system/menu";
import {
  treeselect as deptTreeselect,
  roleDeptTreeselect,
} from "@/api/system/dept";
import add from "../../components/merchant/add.vue";
import { dictFilter } from "@/utils/filters/common";
export default {
  name: "Role",
  dicts: ["sys_normal_disable"],
  filters: { dictFilter },
  components: {
    add,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      operationValue: "",
      stateOptions: [
        {
          value: 1,
          label: "启用",
        },
        {
          value: 2,
          label: "停用",
        },
        {
          value: 3,
          label: "删除",
        },
      ],
      statusList: [
        {
          dictValue: "1",
          dictLabel: "启用",
        },
        {
          dictValue: "2",
          dictLabel: "停用",
        },
      ],
      // 角色表格数据
      supplierList: [
        {
          id: 1, //id
          number: "GY123456", //编号
          companyName: "捷讯达物流公司", //
          unifiedSocialCreditCode: "91440300319254486Y", //统一社会信用代码
          companyNumber: "0751-3753666", //公司电话
          nameLegalPerson: "白居易", //法人姓名
          legalPersonCertificateNo: "121231212121554", //法人证件号
          contactName: "陆游", //联系人姓名
          contactMobileNumber: "13825654587", //联系人手机号码
          contactEmail: "2115454@qq.com", //联系人邮箱
          state: "1", //状态
          founder: "admin", //创建人
          createTime: "2022-04-15 12:00:00", //创建时间
          address: "", //详细地址
          certificateType: "", //证件类型
          accountName: "", //账户名称
          bankAccount: "", //银行账号
          depositBankName: "", //开户行名称
          bankAddress: "", //开户行地址
          countryDomicile: "", //公司所在地
        },
      ],
      open: false,
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,
        unifiedSocialCreditCode: undefined,
        state: undefined,
      },
      info: {
        type: 1,
        id: 0,
      },
      currentDetail: {},
      selectedData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = false;
      this.total = 10;
    },
    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then((response) => {
        this.menuOptions = response.data;
      });
    },
    /** 查询部门树结构 */
    getDeptTreeselect() {
      deptTreeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    // 所有部门节点数据
    getDeptAllCheckedKeys() {
      // 目前被选中的部门节点
      let checkedKeys = this.$refs.dept.getCheckedKeys();
      // 半选中的部门节点
      let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeselect(roleId) {
      return roleMenuTreeselect(roleId).then((response) => {
        this.menuOptions = response.menus;
        return response;
      });
    },
    /** 根据角色ID查询部门树结构 */
    getRoleDeptTreeselect(roleId) {
      return roleDeptTreeselect(roleId).then((response) => {
        this.deptOptions = response.depts;
        return response;
      });
    },
    // 角色状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.roleName + '"角色吗？')
        .then(function () {
          return changeRoleStatus(row.roleId, row.status);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.form = {
        roleId: undefined,
        roleName: undefined,
        roleKey: undefined,
        roleSort: 0,
        status: "0",
        menuIds: [],
        deptIds: [],
        menuCheckStrictly: true,
        deptCheckStrictly: true,
        remark: undefined,
      };
      this.resetForm("form");
    },
    sure(data) {
      this.open = false;
      this.supplierList.push(data);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleView(row) {
      this.info.type = 2; //查看
      this.open = true;
      this.currentDetail = row;
      console.log(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedData = selection;
      this.ids = selection.map((item) => item.roleId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.info.type = 10;
      this.open = true;
      this.title = "添加角色";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.info.type = 3; //修改
      this.open = true;
    },
    operationFun(value) {
      if(value === 1) {
        // 启用
        this.selectedData.map(item => item.state = "1");
      }else if (value === 2) {
        // 停用
        this.selectedData.map(item => item.state = "2");
      }else if (value === 3) {
        // 删除
        const restArr = this.supplierList.filter(item => !this.selectedData.some(subItem => subItem.id === item.id))
        this.supplierList = restArr;
      }
      this.$modal.msgSuccess("操作成功");
    },
    /** 删除按钮操作 */
    handleDelete(scope) {
      let that = this;
      this.$modal
        .confirm("是否确认删除当前数据？")
        .then(function () {
          that.supplierList.splice(scope.$index, 1);
          that.getList();
          that.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/role/export",
        {
          ...this.queryParams,
        },
        `role_${new Date().getTime()}.xlsx`
      );
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      return true;
    },
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3);
    },
    closeFun(val) {
      console.log(val);
      this.open = val;
    },
  },
};
</script>
<style scope  lang="scss" >
.app-container {
}
</style>